// 确认给予npc道具 {"类型":"道具","数量":["1","1","1"],"格子":[8,12,13],"银子":"100000"}
// @ts-ignore
export async function execute(client_id, message_json) {
    let characters = G.Characters.get_current_login_characters(client_id);
    // 是否为剧情给予
    if (characters["给予数据"] && characters["剧情"]["附加"] && characters["剧情"]["附加"]["物品"]
        && characters["给予数据"]["地图"] === characters["剧情"]["地图"] && characters["给予数据"]["编号"] === characters["剧情"]["编号"]) {
        let index_list = message_json["格子"];
        let num_list = message_json["数量"];
        let plot_item_items = characters["剧情"]["附加"]["物品"];
        // 最终需要消耗的物品信息
        let consummer_data = [];
        let consummer_count = 0;
        
        for (let index = 0; index < index_list.length; index++) {
            let num = num_list[index];
            let item_index = index_list[index] - 1;
            if (characters["道具"][item_index] && characters["道具"][item_index]["名称"] === plot_item_items[characters["道具"][item_index]["名称"]]) {
                let item_name = characters["道具"][item_index]["名称"];
                let submit_num = plot_item_items[characters["道具"][item_index]["名称"]];
                let item = characters["道具"][item_index];
                // 校验每个要给予物品的数量
                if (item["可叠加"] && item["数量"] < num) {
                    ClientUtils.send_basic_prompt(client_id, `${item_name}数量不足`)
                    break;
                }
                if (num >= submit_num) {
                    consummer_data.push([item_index, submit_num]);
                    consummer_count++;
                }
            }
        }
        //消耗物品
        if (consummer_count === Object.keys(plot_item_items).length) {
            for (let index = 0; index < consummer_data.length; index++) {
                let consummer = consummer_data[index];
                G.Characters.remove_item(characters["id"], "道具", consummer[0], consummer[1]);
            }
            characters["剧情"]["给予"] = true;
            ClientUtils.send_basic_prompt(client_id, `#G给予成功`)
            characters["给予数据"] = undefined;
            // 自动触发下一步剧情
            let characters_map_id = characters["地图数据"]["编号"];
            let map_npc = G.MapNpcData.default[characters_map_id];
            let npc_data = map_npc[characters["剧情"]["编号"]];
            let plot_dialogue_func = _DialogueData.get("plot_dialogue")[characters["剧情"]["主线"]][characters["剧情"]["进度"]];
            if (plot_dialogue_func !== undefined) {
                let plot_dialogue = plot_dialogue_func(characters, npc_data);
                if (plot_dialogue) {
                    ClientUtils.send_client_dialogue(client_id, plot_dialogue[0], plot_dialogue[1], plot_dialogue[2], plot_dialogue[3], plot_dialogue[4], plot_dialogue[5]);
                    ClientUtils.send_team_dialogue_data(characters, plot_dialogue);
                    return;
                }
            }
        } else {
            let map_npc = G.MapNpcData.default[characters["剧情"]["地图"]];
            let npc_data = map_npc[characters["剧情"]["编号"]];
            ClientUtils.send_client_dialogue(client_id, npc_data["模型"], npc_data["名称"], "少侠在检查一下需要交给我的物品.");
        }
    }
}